package com.beta.restapi.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.common.web.ContentType;
import com.beta.dto.order.RefundItemDto;
import com.beta.restapi.internal.Consts;
import com.beta.restapi.request.order.RejectRequest;
import com.beta.vo.refund.RefundItemPageVO;
import com.beta.vo.refund.RefundItemVO;
import com.beta.service.IOrderItemsService;
import io.swagger.annotations.Api;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by lichao on 2016/11/29.
 */
@Api(tags = "RefundItem")
@RestController
@RequestMapping(Consts.API_PATH_PREFIX + Consts.Resource.REFUND_ITEMS)
public class RefundItemController extends BaseController {

    @Autowired
    private IOrderItemsService orderItemsService;
    @Autowired
    private MapperFacade beanMapper;

    @PatchMapping(value = "{refundItemId}/agree", produces = ContentType.JSON)
    public Object AgreeRefundItem(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                  @PathVariable("refundItemId") Integer refundItemId) {
        orderItemsService.executeAgreeRefundItem(refundItemId);
        return ok();
    }

    @GetMapping(value = "", produces = ContentType.JSON)
    public Object getAllRefundItems(@RequestHeader(Consts.HEADER_AUTH_KEY) String token) {
        Page<RefundItemDto> allRefundItems = orderItemsService.getAllRefundItems(getCurrentUserId(), getMybatisPage());
        return ok(toRefundPageVO(allRefundItems));
    }

    @GetMapping(value = "{refundStatus}", produces = ContentType.JSON)
    public Object getRefundItems(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                 @PathVariable("refundStatus") String refundStatus) {
        Page<RefundItemDto> refundItemsWithStatus = orderItemsService.getRefundItems(getCurrentUserId(), refundStatus, getMybatisPage());
        return ok(toRefundPageVO(refundItemsWithStatus));
    }

    private RefundItemPageVO toRefundPageVO(Page<RefundItemDto> refundItemDtoPage) {
        RefundItemPageVO refundItemPageVO = new RefundItemPageVO();
        List<RefundItemDto> refundItemDtos = refundItemDtoPage.getRecords();
        refundItemPageVO.setRefundItems(beanMapper.mapAsList(refundItemDtos, RefundItemVO.class));
        fillPageParam(refundItemDtoPage, refundItemPageVO);
        return refundItemPageVO;
    }

    @PatchMapping(value = "{orderItemId}/reject",produces = ContentType.JSON)
    public Object executeReject(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                @PathVariable("orderItemId")Integer orderItemId,
                                @RequestBody RejectRequest rejectRequest){
        BigDecimal refundRate = rejectRequest.getRefundRate();
        orderItemsService.executeReject(orderItemId, refundRate, getCurrentUserId());
        return ok();
    }

    @PatchMapping(value = "{orderItemId}/unreject",produces = ContentType.JSON)
    public Object executeUnreject(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                  @PathVariable("orderItemId")Integer orderItemId){
        orderItemsService.executeUnReject(orderItemId,getCurrentUserId());
        return ok();
    }

}
